﻿Imports System.Windows
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Public Class ChangePasswordPageViewModel : Inherits ObservableObject
    Private _userEdited As User
    Public Property UserEdited As User
        Get
            Return _userEdited
        End Get
        Set(value As User)
            SetProperty(_userEdited, value)
        End Set
    End Property

    Private _oldPassword As String
    Public Property OldPassword As String
        Get
            Return _oldPassword
        End Get
        Set(value As String)
            SetProperty(_oldPassword, value)
        End Set
    End Property

    Private _newPassword As String
    Public Property NewPassword As String
        Get
            Return _newPassword
        End Get
        Set(value As String)
            SetProperty(_newPassword, value)
        End Set
    End Property

    Private _comfirmPassword As String
    Public Property ComfirmPassword As String
        Get
            Return _comfirmPassword
        End Get
        Set(value As String)
            SetProperty(_comfirmPassword, value)
        End Set
    End Property

    Private _errorMessage As String
    Public Property ErrorMessage As String
        Get
            Return _errorMessage
        End Get
        Set(value As String)
            SetProperty(_errorMessage, value)
        End Set
    End Property

    Private ReadOnly _client As HttpClientBase

    Public Property SaveProfileCommand As New AsyncRelayCommand(AddressOf SaveProfileClicked)
    Public Property CancelCommand As New RelayCommand(AddressOf CancelClicked)

    Public Sub New()
        '从服务器获取用户信息
        _client = New HttpClientBase()
        Call InitProfile()
    End Sub

    Private Async Function InitProfile() As Task
        Try
            Dim response = Await _client.GetAsync(Of User)("vdm/api/account/findUser/id/" + Application.Current.Resources("UserId").ToString())
            If response.Code <> 200 Then
                MessageBox.Show("登录状态异常导致获取用户信息失败，请重新登录")
                NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
                Return
            End If
            UserEdited = response.Data
        Catch ex As Exception
            MessageBox.Show($"系统错误：{ex.Message}")
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try
    End Function

    Private Async Function SaveProfileClicked() As Task
        '如果旧密码错误
        If OldPassword <> UserEdited.Password Then
            ErrorMessage = "旧密码错误，请重新输入"
            Return
        End If
        '如果新密码不为空，且位于 8 到 15 位之间
        If Not String.IsNullOrWhiteSpace(NewPassword) AndAlso NewPassword.Length > 7 AndAlso NewPassword.Length < 16 Then
            '如果两次密码输入一致
            If NewPassword = ComfirmPassword Then
                '修改密码
                UserEdited.Password = NewPassword
                '调用服务端接口修改密码
                Dim response = Await _client.PostAsync(Of User)(UserEdited, "vdm/api/account/editUser")
                If response.Code = 200 Then
                    '修改成功，退出重新登录
                    MessageBox.Show("修改密码成功，请重新登录")
                    Application.Current.Resources.Remove("UserId")
                    NavigationService.Navigate(Of LoginPage)()
                Else
                    ErrorMessage = response.Message
                End If
            Else
                ErrorMessage = "两次密码输入不一致，请重新输入"
            End If
        Else
            ErrorMessage = "新密码不能为空且位于 8 到 15 位之间"
        End If
    End Function

    Private Sub CancelClicked()
        '取消编辑，返回到个人中心页面
        NavigationService.Navigate(NavigationData.MAIN_LAYOUT_URI)
    End Sub
End Class
